<?php
namespace app\admin\controller;
use app\admin\business\AdminUsers;
use app\common\model\mysql\AdminUser;
use think\facade\View;

class Login extends Common {

    public function initialize()
    {
        if($this->isLogin()){
            return $this->redirect(url('admin/index/index'));
        }
    }

    public function index(){

        return View::fetch();
    }

    public function md5(){
        halt(session(config('admin.session_user')));

        echo md5('admin-singwa-abc');
    }
    /*
     * 检查
     */
     public function check(){
         if(!$this->request->isPost()){
             return show(config('status.error'),'请求错误');
         }
         //参数校验
         $username = $this->request->param('username','','trim');
         $pwd = $this->request->param('password','','trim');
         $captcha = $this->request->param('captcha','','trim');
//         if(empty($username) || empty($pwd) || empty($captcha)){
//             return show(config('status.error'),'参数不能为空');
//         }
         //验证码是否正确 要开启session thinkphp6默认不开启session
//         if(!captcha_check($captcha)){
//             return show(config('status.error'),'验证码不正确');
//         }
         $data = [
             'username'=>$username,
             'password'=>$pwd,
             'captcha'=>$captcha
         ];
         $validate = new \app\admin\validate\AdminUser();
         if(!$validate->check($data)){
             return show(config('status.error'),$validate->getError());
         }
         try{
             AdminUsers::login($data);
         }catch (\Exception $e){
             return show(config('status.error'),$e->getMessage());
         }
         return show(config('status.success_login'),'登录成功');
     }


}
